package com.dadie.dorm.dao.impl;


import com.dadie.dorm.dao.IHouseDao;
import com.dadie.dorm.pojo.House;
import com.dadie.dorm.pojo.query.HouseQuery;
import com.dadie.dorm.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HouseDaoImpl implements IHouseDao {
    @Override
    public List<House> selectByPage(HouseQuery houseQuery) {
        int offset = (houseQuery.getPage() - 1) * houseQuery.getLimit();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<House> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            // SELECT id,name,password,age,gender FROM house where name like ? and gender=? limit ?,?
            // SELECT id,name,password,age,gender FROM house where name like ? limit ?,?
            // SELECT id,name,password,age,gender FROM house where gender=? limit ?,?
            // SELECT id,name,password,age,gender FROM house limit ?,?
            //你有这个条件，我们就拼上这个条件 and ...， 但是第一个又没有and
            // String sql = "select * from house where 1=1 "
            // if(name is not null) {
            //      sql += "and name like ?"
            // }
            // if(gender is not null) {
            //      sql += "and gender=?"
            // }
            String sql = "SELECT id,name,phone FROM house where 1=1 ";
            //这里面放所有搜索条件
            List<Object> queryList = new ArrayList<>();
            String queryName = houseQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add("%" + queryName + "%");
            }

            sql += " limit ?,?";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            preparedStatement.setInt(queryList.size() + 1, offset);
            preparedStatement.setInt(queryList.size() + 2, houseQuery.getLimit());

            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String phone = resultSet.getString("phone");



                House house = new House(id, name, phone);
                list.add(house);
            }
            for (House house : list) {
                System.out.println(house);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Integer selectTotalCount(HouseQuery houseQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from house where 1=1 ";
            List<Object> queryList = new ArrayList<>();
            String queryName = houseQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add(queryName);
            }

            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from house where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int add(House house) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into house(name,phone) values(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, house.getName());
            preparedStatement.setString(2, house.getPhone());


            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public House getHouseUpdatePage(String id) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        House house=null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,phone from house where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.parseInt(id));
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                String name = resultSet.getString("name");
                String phone = resultSet.getString("phone");

                house = new House(Integer.parseInt(id),name,phone);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }
        return house;
    }

    @Override
    public House selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        House house = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,phone FROM house where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                String name = resultSet.getString("name");
                String phone = resultSet.getString("phone");

                house = new House(id, name,phone);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return house;
    }


    @Override
    public int update(House house) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count=0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update house set name=?,phone=? where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, house.getName());
            preparedStatement.setString(2, house.getPhone());

            preparedStatement.setInt(3,house.getId());
            count=preparedStatement.executeUpdate();
            System.out.println(preparedStatement);
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }


}
